package com.sumainfor.pc.market.service;

import com.sumainfor.pc.market.dao.CityMapper;
import com.sumainfor.pc.market.dao.DistrictMapper;
import com.sumainfor.pc.market.dao.ProvinceMapper;
import com.sumainfor.pc.market.dao.StreetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 * 查询省市区
 * @Auther: zhlu
 * @Date: 2019/7/2 21:26
 * @Description:
 */
@Service
public class areaService {

    @Autowired
    ProvinceMapper ProvinceMapper;

    @Autowired
    CityMapper CityMapper;

    @Autowired
    DistrictMapper DistrictMapper;

    @Autowired
    StreetMapper StreetMapper;

    /**
     * 功能描述:
     * 获取已创建的省市区
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/7/2 21:28
     */
    public List<Map<String,Object>> getAllAreaList(){
        List<Map<String,Object>>AllAreaList=new ArrayList<Map<String,Object>>();
        //获取所有的省份
        List<Map<String,Object>>getProvinceList=ProvinceMapper.getProvinceList(new HashMap<String,Object>(){{
            put("ProvinceID","310000");
        }});
        if(getProvinceList.size()>0){
            //循环省份
            Map<String,Object>proMap;
            for(Map<String,Object> pro:getProvinceList){
                proMap=new HashMap<String,Object>();
                proMap.put("id",pro.get("ProvinceID"));
                proMap.put("label",pro.get("Province"));
                List<Map<String,Object>>proChildren=new ArrayList<Map<String,Object>>();
                //根据省份编号获取所有的城市
                List<Map<String,Object>>CityList=CityMapper.getCityList(pro);
                if(CityList.size()>0){
                    //循环城市
                    Map<String,Object>cityMap;
                    for(Map<String,Object> City:CityList){
                        cityMap=new HashMap<String,Object>();
                        cityMap.put("id",City.get("CityID"));
                        cityMap.put("label",City.get("City"));
                        List<Map<String,Object>>cityChildren=new ArrayList<Map<String,Object>>();
                        //根据城市编号获取所有的区
                        List<Map<String,Object>>DistrictList=DistrictMapper.getDistrictList(City);
                        if(DistrictList.size()>0){
                            //循环区
                            Map<String,Object>districtMap;
                            for(Map<String,Object> District:DistrictList){
                                districtMap=new HashMap<String,Object>();
                                districtMap.put("id",District.get("DistrictID"));
                                districtMap.put("label",District.get("District"));
                                List<Map<String,Object>>districtChildren=new ArrayList<Map<String,Object>>();
                                //根据区编号获取所有的街道
                                List<Map<String,Object>>StreetList=StreetMapper.getStreetList(District);
//                                if(StreetList.size()>0){
////                                    //循环街道
//                                    Map<String,Object>streetMap;
//                                    for(Map<String,Object> street:StreetList){
//                                        streetMap=new HashMap<String,Object>();
//                                        streetMap.put("id",street.get("StreetID"));
//                                        streetMap.put("label",street.get("Street"));
//                                        districtChildren.add(streetMap);
//                                    }
//                                }
                                districtMap.put("children",districtChildren);
                                cityChildren.add(districtMap);
                            }
                        }
                        cityMap.put("children",cityChildren);
                        proChildren.add(cityMap);
                    }
                }
                proMap.put("children",proChildren);
                AllAreaList.add(proMap);
            }
        }
        return AllAreaList;
    }

}
